# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/topics/item-pipeline.html
from scrapy import signals

class FreshPipeline(object):


    @classmethod
    def from_crawler(cls, crawler):
        pipeline = cls()
        crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
        return pipeline
        
    def process_item(self, item, spider):
        if spider.name == "tenedorhist":
            spider.db.insertRestaurants(item,spider.db.UPDATE)
        elif spider.name == "tenedormaster":
            spider.db.insertRestaurants(item,spider.db.INSERT)
        elif spider.name == "atrapalohist":
            spider.db.insertRestaurants(item,spider.db.UPDATE)
        elif spider.name == "atrapalomaster":
            spider.db.insertRestaurants(item,spider.db.INSERT)
        elif spider.name == "restalohist":
            spider.db.insertRestaurants(item,spider.db.UPDATE)
        elif spider.name == "restalomaster":
            spider.db.insertRestaurants(item,spider.db.INSERT)
        elif spider.name == "salirhist":
            spider.db.insertRestaurants(item,spider.db.UPDATE)
        elif spider.name == "salirmaster":
            spider.db.insertRestaurants(item,spider.db.INSERT)
        elif spider.name == "yelphist":
            spider.db.insertRestaurants(item,spider.db.UPDATE)
        elif spider.name == "yelpmaster":
            spider.db.insertRestaurants(item,spider.db.INSERT)
        elif spider.name == "triphist":
            spider.db.insertRestaurants(item,spider.db.UPDATE)
        elif spider.name == "tripmaster":
            spider.db.insertRestaurants(item,spider.db.INSERT)
        elif spider.name == "paginasmaster":
            """insert done within the spider"""
            return
        else:
            spider.db.insertRestaurants(item)
        return

    def spider_closed(self, spider):
        spider.close_spider()
        return
